package com.ftf.tp.blog.interfaces.controller;

import com.ftf.tp.blog.application.dto.QuestionPageDTO;
import com.ftf.tp.blog.application.dto.command.QuestionCommand;
import com.ftf.tp.blog.application.dto.command.WriteAnswerCommand;
import com.ftf.tp.blog.application.service.representation.QuestionRepresentation;
import com.ftf.tp.blog.application.vo.QuestionVO;
import com.ftf.tp.common.core.annotation.ResponseResult;
import com.ftf.tp.common.core.utils.CommonUtil;
import com.ftf.tp.common.cqrs.handler.Dispatcher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author tanghao
 * @date 2022/8/11
 */
@Slf4j
@RestController
@ResponseResult
@RequestMapping("/configure")
@Api(tags = "配置Controller")
public class ConfigureController {


    @Resource(name = "standardDispatcher")
    private Dispatcher standardDispatcher;

    @Resource
    private QuestionRepresentation questionRepresentation;

    /**
     * 提问
     */
    @ApiOperation("提问")
    @PostMapping("/auth/ask")
    public void publishArticle(@RequestBody @Validated QuestionCommand questionCommand, BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        standardDispatcher.dispatch(questionCommand);
    }

    /**
     * 等你来答
     */
    @ApiOperation("等你来答")
    @PostMapping("/question/page")
    public List<QuestionVO> pageQuestion(@RequestBody QuestionPageDTO questionPageDTO) {
        return questionRepresentation.pageQuestion(questionPageDTO);
    }

    @ApiOperation("写回答")
    @PostMapping("/auth/writeAnswer")
    public void writeAnswer(@RequestBody @Validated WriteAnswerCommand writeAnswerCommand,BindingResult bindingResult) {
        CommonUtil.paramValid(bindingResult);
        standardDispatcher.dispatch(writeAnswerCommand);
    }
}
